package com.qf.exception;/*
亲元宝 2019/12/13  10:10
/**
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 * .............................................
 *          写字楼里写字间，写字间里程序员；
 *          程序人员写程序，又拿程序换酒钱。
 *          酒醒只在网上坐，酒醉还来网下眠；
 *          酒醉酒醒日复日，网上网下年复年。
 *          但愿老死电脑间，不愿鞠躬老板前；
 *          奔驰宝马贵者趣，公交自行程序员。
 *          别人笑我忒疯癫，我笑自己命太贱；
 *          不见满街漂亮妹，哪个归得程序员？
 */


import com.alibaba.fastjson.JSON;
import com.qf.util.AjaxUtil;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;



@Component
public class Myexception implements HandlerExceptionResolver {
    private Logger logger = Logger.getLogger(Myexception.class);
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response, Object handler, Exception ex) {
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try {
            String errorMsg="";
            boolean isAjax= "1".equals(request.getParameter("isAjax"));

            //ex 为业务层抛出的自定义异常
            if(ex instanceof ServiceCustomException){
                ServiceCustomException customEx=    (ServiceCustomException)ex;

                errorMsg ="customStatus:"+customEx.getCustomStatus() +",customMessage:"+customEx.getCustomMessage()
                        +"\r\n"+ ExceptionUtils.getStackTrace(customEx);
                logger.error(errorMsg);
            }else{
                //ex为非自定义异常，则
                errorMsg=ExceptionUtils.getStackTrace(ex);
                logger.error(errorMsg);

            }

            if(isAjax){

                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().write(JSON.toJSONString(AjaxUtil.messageMap(500, errorMsg)));
                return new   ModelAndView();
            }else{
                //否则，  输出错误信息到自定义的500.jsp页面
                ModelAndView mv = new ModelAndView("/error/500.jsp");
                mv.addObject("errorMsg", errorMsg);
                return mv ;
            }
        } catch (IOException e) {
            logger.error(ExceptionUtils.getStackTrace(e));
        }
        return new   ModelAndView();

    }
}
